<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <input type="text">

    <script>
        var inp = document.querySelector("input");
        inp.onkeypress = function(e) {
            var code = e.key[0].charCodeAt(0);
            if (code < 48 || code > 57) {
                e.preventDefault();
            }
        }

        inp.onpaste = function(e) {
            var text = e.clipboardData.getData("text/plain");
            if (!text || !/\d+/.test(text)) {
                e.preventDefault();
            }
        }
    </script>
</body>

</html>